#include "VideoRecordTool.h"
#include "FilePicker.h"

VideoRecordTool::VideoRecordTool(QObject *parent)
    : QObject{parent}
{}

void VideoRecordTool::startRecord() {
    if (this->isRecording == false) {

        this->recordThread = nullptr;

        this->recordThread = new VideoRecordThread();

        QString recordPath = FilePicker::locationPath(DesktopDir).append("/record/").append(QDateTime::currentDateTime().toString("yyyy_MM_dd_HHmmss")).append(".yuv");
        this->recordThread->recordPath = recordPath;

        connect(this->recordThread, &VideoRecordThread::finished, this,
                [this]() { // 线程结束

                    qDebug() << "线程结束了";

                    this->recordThread = nullptr;
                    this->isRecording = false;
                });
        connect(this->recordThread, &VideoRecordThread::recordState, this, [this](VideoRecordState state) {
            switch (state) {
            case VideoRecording:
            {
                emit this->recordState(VideoRecording, 0);
            }
            break;
            case VideoRecordEnd:
            {
                this->isRecording = false;
            }
            break;

            case VideoRecordFail:
            {
                emit this->recordState(VideoRecordFail, 0);
                this->recordThread->requestInterruption();
                this->recordThread = nullptr;
                this->isRecording = false;
            }
            break;

            default:
                break;
            }
        });
        this->recordThread->start();
    }
}

void VideoRecordTool::stopRecord() {
    this->recordThread->requestInterruption();
    this->recordThread = nullptr;
    this->isRecording = false;
}
